<?php

use console\db\Migration;

/**
 * 创建橙播相关信息表
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m190102_180239_t_live extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 直播内容表
         * @table `live`
         */
        $this->tableBuilder([
            'name' => 'live',
            'comment' => '直播内容表',
            'columns' => [
                'id' => $this->primaryKey(),
                'title' => $this->string(50)->comment('标题'),
                'description' => $this->string(255)->defaultValue('')->comment('描述'),
                'cover' => $this->string(255)->comment('封面'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('主播ID'),
                'type_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('类型ID'),
                'room_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('直播间ID'),
                //
                'start_time' => $this->unixTimestamp()->comment('开始时间'),
                'stop_time' => $this->unixTimestamp()->comment('结束时间'),
                //
                'recommend_at' => $this->unixTimestamp()->comment('推荐时间'),
                'free_time' => $this->smallInteger(6)->unsigned()->notNull()->defaultValue(0)->comment('非vip可免费观看秒数, 0为全部可看'),
                'weight' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('权重'),
                'best_at' => $this->unixTimestamp()->comment('设为精华时间'),
                //
                'location' => $this->string(50)->defaultValue('')->comment('所在地'),
                'gis' => $this->geometry()->comment('空间位置信息'),
                'geohash' => $this->geometryHash('gis'),
                //
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);

        /**
         * 直播间表
         * @table `live_room`.
         */
        $this->tableBuilder([
            'name' => 'live_room',
            'comment' => '直播间表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->char(50)->notNull()->comment('应用名称'),
                'description' => $this->string(255)->defaultValue('')->comment('描述'),
                'scheme' => $this->char(20)->notNull()->defaultValue('rmtp')->comment('类型名称'),
                'channel' => $this->char(20)->defaultValue('aliyun')->comment('推流通道'),
                'push' => $this->char(255)->defaultValue('')->comment('推流地址'),
                'pull' => $this->char(255)->defaultValue('')->comment('拉流地址'),
                'auth_key' => $this->char(32)->defaultValue('')->comment('通道密钥'),
                'aux_key' => $this->char(32)->defaultValue('')->comment('备用密钥'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);

        /**
         * 直播类型表
         * @table `live_type`.
         */
        $this->tableBuilder([
            'name' => 'live_type',
            'comment' => '直播类型表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(30)->notNull()->comment('类型名称'),
                'icon' => $this->char(255)->comment('类型图标'),
                'system' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('系统直播'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);

        /**
         * 直播观看用户日志表
         * @table `live_watch`.
         */
        $this->tableBuilder([
            'name' => 'live_watch',
            'comment' => '直播观看用户日志表',
            'columns' => [
                'id' => $this->primaryKey(),
                'live_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('直播ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'duration' => $this->unixTimestamp()->comment('观看时长'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);

        /**
         * 直播统计表
         * @table `live_stats`.
         */
        $this->tableBuilder([
            'name' => 'live_stats',
            'comment' => '直播统计表',
            'columns' => [
                'id' => $this->primaryKey(),
                'live_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('节目ID'),
                'room_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('直播间ID'),
                'views' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('总观看数'),
                'likes' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('点赞数'),
                'comments' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论数'),
                'shares' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('分享数'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%live_stats}}');
        $this->dropTable('{{%live_watch}}');
        $this->dropTable('{{%live_type}}');
        $this->dropTable('{{%live_room}}');
        $this->dropTable('{{%live}}');
    }

}
